/*
 * Async Treeview 0.1 - Lazy-loading extension for Treeview
 * 
 * http://bassistance.de/jquery-plugins/jquery-plugin-treeview/
 *
 * Copyright (c) 2007 Jörn Zaefferer
 * Portions Copyright (c) 2010 Marnix van Valen / Alanta
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Revision: $Id$
 *
 */

;(function($) {

    function load(settings, root, child, container) {
        $.getJSON(settings.url + '/' + root, null, function(response) {
            function createNode(parent) {
                var current = $("<li/>").html("<span>" + this.text + "</span>").appendTo(parent);
                if( this.path ) current.data('path', this.path);
                if (this.type) {
                    current.children("span").addClass(this.type);
                }
                if (this.expanded) {
                    current.addClass("open");
                }
                if (this.hasChildren || this.children && this.children.length) {
                    var branch = $('<ul style="display:none"/>').appendTo(current);
                    if (this.hasChildren) {
                        current.addClass("hasChildren");
                        createNode.call({
                            text: "placeholder",
                            id: "placeholder",
                            children: []
                        }, branch);
                    }
                    if (this.children && this.children.length) {
                        $.each(this.children, createNode, [branch])
                    }
                }
            }
            $.each(response, createNode, [child]);
            $(container).treeview({ add: child });
        });
    }

    var proxied = $.fn.treeview;
    $.fn.treeview = function(settings) {
        if (!settings.url) {
            return proxied.apply(this, arguments);
        }
        var container = this;
        if (!(this.children && this.children.length)) {
        load(settings, "", this, container);
        }
        else {
            $('li', this).each(function() {
                var current = $(this);
                if (current.attr('path')) {
                    current.data('path', current.attr('path'));
                    current.attr('path', '');
                }
            });
            $('ul>li:last').addClass('last');
        }
        var userToggle = settings.toggle;
        return proxied.call(this, $.extend({}, settings, {
            collapsed: $(this).hasClass("hasChildren"),
            toggle: function() {
                var current = $(this);
                if (current.hasClass("hasChildren")) {
                    var childList = current.removeClass("hasChildren").find("ul");
                    childList.empty();
                    load(settings, current.data('path'), childList, container);
                }
                if (userToggle) {
                    userToggle.apply(this, arguments);
                }
            }
        }));
    };

})(jQuery);